package com.xlcszsgc.module.resourceimport.ui.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xlcszsgc.core.page.CalculatePage;
import com.xlcszsgc.core.servicetemplate.treetemplate.jsonbean.JsonTreeBean;
import com.xlcszsgc.module.blob.service.IBlobService;
import com.xlcszsgc.module.blob.ui.commond.BaseBlobCommond;
import com.xlcszsgc.module.resourceimport.ui.bean.ImgFile;
import com.xlcszsgc.module.resourceimport.ui.commond.ImageCommond;

@Controller
@RequestMapping(value = {ShowImageList.modulePath,ShowImageList.portalPath,ShowImageList.anliPath})
public class ShowImageList {
	
	public final static String modulePath="/module/image";
	public static final String portalPath = "/portal";
	public static final String anliPath = "/module/anli";
	@Resource(name = "com.xlcszsgc.module.blob.service.impl.BlobServiceImpl")
	private IBlobService blobService;
	
	
	@RequestMapping("/delImage")
	public  String delImagFile(Model model, HttpServletRequest request,ImageCommond commond) throws Exception{
		String foldName=request.getParameter("imgKind");
		String rootPath=request.getSession().getServletContext().getRealPath("/");
		if(rootPath.contains("/")){
			rootPath+="image/"+foldName+"/";
		}else{
			rootPath+="image\\"+foldName+"\\";
		}
		String [] names=commond.getSearchImgNames();
		for(int i=0;i<names.length;i++){
			File imgFile=new File(rootPath+names[i]);
			if(imgFile.exists()){
				imgFile.delete();
			}
		}
		return modulePath+"/listImage.do";
	}
	
	@RequestMapping("/listImage")
	public String showImgList(Model model, HttpServletRequest request,ImageCommond commond) throws Exception{
		String foldName=request.getParameter("imgKind");
		String imgName=request.getParameter("imgName");
		commond.setImgKind(foldName);
		String rootPath=request.getSession().getServletContext().getRealPath("/");
		String path="";
		String prePath="";
		if(rootPath.contains("/")){
			rootPath+="image/"+foldName+"/";
			
		}else{
			rootPath+="image\\"+foldName+"\\";
		}
		path="/image/"+foldName+"/";
		prePath="../..";
		File file=new File(rootPath);
		if(file.isDirectory()){
			File[] fileList=file.listFiles();
			if(fileList!=null&&fileList.length>0){
				List<ImgFile> imgList=new ArrayList<ImgFile>();
				if(imgName!=null&&!"".equals(imgName)){
					List<File> searchImgList=new ArrayList<File>();
					for(File f:fileList){
						if(f.getName().contains(imgName)){
							searchImgList.add(f);
						}
					}
					CalculatePage.calculate(commond.getPageBean(), searchImgList.size());
					for(int i=0;i<searchImgList.size();i++){
						if(i>=(commond.getPageBean().getCurrent()-1)*commond.getPageBean().getRowCount()&&i<(commond.getPageBean().getCurrent()-1)*commond.getPageBean().getRowCount()+commond.getPageBean().getRowCount()){
							File f=searchImgList.get(i);
							if(f.isFile()){
								ImgFile img=new ImgFile();
								img.setImgName(f.getName());
								img.setImgPath(prePath+path+f.getName());
								imgList.add(img);
							}
						}
					}
				}else{
					CalculatePage.calculate(commond.getPageBean(), fileList.length);
					for(int i=0;i<fileList.length;i++){
						if(i>=(commond.getPageBean().getCurrent()-1)*commond.getPageBean().getRowCount()&&i<(commond.getPageBean().getCurrent()-1)*commond.getPageBean().getRowCount()+commond.getPageBean().getRowCount()){
							File f=fileList[i];
							if(f.isFile()){
								ImgFile img=new ImgFile();
								img.setImgName(f.getName());
								img.setImgPath(prePath+path+f.getName());
								imgList.add(img);
							}
						}
					}
				}
				commond.setResultCollection(imgList);
			}else{
				commond.setResultCollection(null);
			}
		}else{
			commond.setResultCollection(null);
		}
		model.addAttribute("imageCommond", commond);
		return modulePath+"/listImage.jsp";
	}
	
	@RequestMapping("/listFrontImage")
	public String showImgFrontList(Model model, HttpServletRequest request,ImageCommond commond) throws Exception{
		String foldName=request.getParameter("imgKind");
		commond.setImgKind(foldName);
		String rootPath=request.getSession().getServletContext().getRealPath("/");
		String path="";
		String prePath="";
		if(rootPath.contains("/")){
			rootPath+="image/"+foldName+"/";
			
		}else{
			rootPath+="image\\"+foldName+"\\";
		}
		path="/image/"+foldName+"/";
		prePath="..";
		File file=new File(rootPath);
		if(file.isDirectory()){
			File[] fileList=file.listFiles();
			if(fileList!=null&&fileList.length>0){
				List<ImgFile> imgList=new ArrayList<ImgFile>();
					CalculatePage.calculate(commond.getPageBean(), fileList.length);
					for(int i=0;i<fileList.length;i++){
						if(i>=(commond.getPageBean().getCurrent()-1)*commond.getPageBean().getRowCount()&&i<(commond.getPageBean().getCurrent()-1)*commond.getPageBean().getRowCount()+commond.getPageBean().getRowCount()){
							File f=fileList[i];
							if(f.isFile()){
								ImgFile img=new ImgFile();
								img.setImgName(f.getName());
								img.setImgPath(prePath+path+f.getName());
								imgList.add(img);
							}
						}
					}
				commond.setResultCollection(imgList);
			}else{
				commond.setResultCollection(null);
			}
		}else{
			commond.setResultCollection(null);
		}
		model.addAttribute("imageCommond", commond);
		return "/portal/listFrontImage.jsp";
	}
	
	@RequestMapping(value = "/findImageTree")
	public @ResponseBody
	List<JsonTreeBean> findResourceTree(Model model, HttpServletRequest request)
			throws Exception {
		List<JsonTreeBean> treeBeanList=new ArrayList<JsonTreeBean>();
		String id=request.getParameter("fid");
		if(id==null||"".equals(id)){
			JsonTreeBean jt=this.buildJsonTreeBean("1","-2","图片分类","closed");
			treeBeanList.add(jt);
		}else{
			JsonTreeBean jt1=this.buildJsonTreeBean("1001","1","家居室内装饰","open");
			JsonTreeBean jt2=this.buildJsonTreeBean("1002","1","办公室案例","open");
			JsonTreeBean jt3=this.buildJsonTreeBean("1003","1","酒店宾馆案例","open");
			JsonTreeBean jt4=this.buildJsonTreeBean("1004","1","货柜展柜案例","open");
			JsonTreeBean jt5=this.buildJsonTreeBean("1005","1","案例展示","open");
			treeBeanList.add(jt1);
			treeBeanList.add(jt2);
			treeBeanList.add(jt3);
			treeBeanList.add(jt4);
			treeBeanList.add(jt5);
		}
		return treeBeanList;
	}
	
	private JsonTreeBean buildJsonTreeBean(String id,String parentId,String text,String state){
		JsonTreeBean bean =new JsonTreeBean();
		bean.setId(id);
		bean.setState(state);
		bean.setParentID(parentId);
		bean.setText(text);
		return bean;
	}
	
	@RequestMapping(value="/showAnliImg")
	public String showAnLiImg(Model model, 
			HttpServletRequest request, BaseBlobCommond baseBlobCommond)throws Exception{
		
		Long count=blobService.getCountBaseBlob(baseBlobCommond);
		CalculatePage.calculate(baseBlobCommond.getPageBean(), count.intValue());
		
		blobService.getResBaseBlob(baseBlobCommond);
		request.setAttribute("baseCommond", baseBlobCommond);
		return anliPath+"/showAnLiImg.jsp";
	}
	
	@RequestMapping(value="/delAnliImg")
	public String delAnLiImg(Model model, 
			HttpServletRequest request, BaseBlobCommond baseBlobCommond)throws Exception{
		if(baseBlobCommond.getSearchBlobIds()!=null&&baseBlobCommond.getSearchBlobIds().length>0){
			blobService.batchdeleteClobById(baseBlobCommond);
		}
		return anliPath+"/showAnliImg.do";
	}
	
}
